package com.zygh.policeasset.utils;

import com.sun.xml.bind.v2.runtime.reflect.opt.Const;
import com.zygh.policeasset.pojo.Basics;
import com.zygh.policeasset.pojo.DailyAssetExcel;
import com.zygh.policeasset.pojo.DepRate;
import com.zygh.policeasset.pojo.Hardwit;
import com.zygh.policeasset.pojo.PurchaseExcel;
import com.zygh.policeasset.pojo.Report;
import com.zygh.policeasset.pojo.Softwit;
import com.zygh.policeasset.pojo.TypeName;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/utils/ExcelUtil.class */
public class ExcelUtil {
    public String exprotExcel(String filePath, String type, Map map) throws Exception {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        TypeName typeName = new TypeName();
        if ("zn".equals(type)) {
            String[] rowName = typeName.getZn1();
            XSSFSheet sheet1 = wb.createSheet("智能资产硬件");
            setSheetHeader(sheet1.createRow(0), rowName, style);
            insertHardwit(sheet1, rowName, (List) map.get("hardwitList"), style);
            String[] rowName2 = typeName.getZn2();
            XSSFSheet sheet2 = wb.createSheet("智能资产软件");
            setSheetHeader(sheet2.createRow(0), rowName2, style);
            insertSoftwit(sheet2, rowName2, (List) map.get("softwitList"), style);
        } else if ("zn1".equals(type)) {
            String[] rowName3 = typeName.getZn1();
            XSSFSheet sheet12 = wb.createSheet("智能资产硬件");
            setSheetHeader(sheet12.createRow(0), rowName3, style);
            insertHardwit(sheet12, rowName3, (List) map.get("hardwitList"), style);
        } else if ("zn2".equals(type)) {
            String[] rowName4 = typeName.getZn2();
            XSSFSheet sheet13 = wb.createSheet("智能资产软件");
            setSheetHeader(sheet13.createRow(0), rowName4, style);
            insertSoftwit(sheet13, rowName4, (List) map.get("softwitList"), style);
        } else if ("jc".equals(type)) {
            String[] rowName5 = typeName.getJc();
            XSSFSheet sheet14 = wb.createSheet("基础资产");
            setSheetHeader(sheet14.createRow(0), rowName5, style);
            insertBasics(sheet14, rowName5, (List) map.get("basicsList"), style);
        } else if ("zd".equals(type)) {
            String[] rowName6 = typeName.getCg();
            XSSFSheet sheet15 = wb.createSheet("采购资产");
            setSheetHeader(sheet15.createRow(0), rowName6, style);
            insertPurchase(sheet15, rowName6, (List) map.get("purchaseList"), style);
        } else if ("da".equals(type)) {
            String[] rowName7 = typeName.getDa();
            XSSFSheet sheet16 = wb.createSheet("日常资产");
            setSheetHeader(sheet16.createRow(0), rowName7, style);
            insertDailyProject(sheet16, rowName7, (List) map.get("dailyAssetList"), style);
        }
        return writeExcelToDisk(filePath, wb);
    }

    public String getReport(String filePath, List<Report> reportList) throws Exception {
        XSSFWorkbook wb = new XSSFWorkbook();
        String[] rowName = new TypeName().getReport();
        XSSFSheet sheet = wb.createSheet("资产报表");
        XSSFRow row = sheet.createRow(0);
        XSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        setSheetHeader(row, rowName, style);
        insertReport(sheet, rowName, reportList, style);
        return writeExcelToDisk(filePath, wb);
    }

    public String getFindAllByDep(String filePath, Map map) throws Exception {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        String[] rowName = new TypeName().getDepRate();
        XSSFSheet sheet1 = wb.createSheet("智能硬件折旧");
        setSheetHeader(sheet1.createRow(0), rowName, style);
        insertDepRate(sheet1, rowName, (List) map.get("hardwitDepRateList"), style);
        XSSFSheet sheet2 = wb.createSheet("智能软件折旧");
        setSheetHeader(sheet2.createRow(0), rowName, style);
        insertDepRate(sheet2, rowName, (List) map.get("softwitDepRateList"), style);
        XSSFSheet sheet22 = wb.createSheet("基础折旧");
        setSheetHeader(sheet22.createRow(0), rowName, style);
        insertDepRate(sheet22, rowName, (List) map.get("basicsDepRateList"), style);
        XSSFSheet sheet23 = wb.createSheet("采购折旧");
        setSheetHeader(sheet23.createRow(0), rowName, style);
        insertDepRate(sheet23, rowName, (List) map.get("purchaseDepRateList"), style);
        return writeExcelToDisk(filePath, wb);
    }

    private void setSheetHeader(XSSFRow row, String[] rowName, XSSFCellStyle style) {
        for (int i = 0; i < rowName.length; i++) {
            XSSFCell cell = row.createCell((int) ((short) i));
            cell.setCellStyle(style);
            cell.setCellValue(rowName[i]);
        }
    }

    private void insertHardwit(XSSFSheet sheet, String[] rowName, List<Hardwit> hardwitList, XSSFCellStyle style) throws IllegalAccessException {
        for (int i = 0; i < hardwitList.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            Hardwit hardwit = hardwitList.get(i);
            Field[] fields = hardwit.getClass().getDeclaredFields();
            for (int j = 4; j < rowName.length + 4; j++) {
                Field f = fields[j];
                f.setAccessible(true);
                XSSFCell cell = row.createCell(((short) j) - 4);
                cell.setCellStyle(style);
                if (f.get(hardwit) != null) {
                    cell.setCellValue(f.get(hardwit).toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }
    }

    private void insertSoftwit(XSSFSheet sheet, String[] rowName, List<Softwit> softwitList, XSSFCellStyle style) throws IllegalAccessException {
        for (int i = 0; i < softwitList.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            Softwit softwit = softwitList.get(i);
            Field[] fields = softwit.getClass().getDeclaredFields();
            for (int j = 4; j < rowName.length + 2; j++) {
                Field f = fields[j];
                f.setAccessible(true);
                if (j < 6) {
                    XSSFCell cell = row.createCell(((short) j) - 4);
                    cell.setCellStyle(style);
                    if (f.get(softwit) != null) {
                        cell.setCellValue(f.get(softwit).toString());
                    } else {
                        cell.setCellValue("");
                    }
                } else if (j == 6) {
                    XSSFCell cell2 = row.createCell(2);
                    cell2.setCellStyle(style);
                    cell2.setCellValue("中心平台");
                    XSSFCell cell3 = row.createCell(3);
                    cell3.setCellStyle(style);
                    cell3.setCellValue("软件系统");
                    XSSFCell cell4 = row.createCell(((short) j) - 2);
                    cell4.setCellStyle(style);
                    if (f.get(softwit) != null) {
                        cell4.setCellValue(f.get(softwit).toString());
                    } else {
                        cell4.setCellValue("");
                    }
                } else if (j > 6) {
                    XSSFCell cell5 = row.createCell(((short) j) - 2);
                    cell5.setCellStyle(style);
                    if (f.get(softwit) != null) {
                        cell5.setCellValue(f.get(softwit).toString());
                    } else {
                        cell5.setCellValue("");
                    }
                }
            }
        }
    }

    private void insertBasics(XSSFSheet sheet, String[] rowName, List<Basics> basicsList, XSSFCellStyle style) throws IllegalAccessException {
        for (int i = 0; i < basicsList.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            Basics basics = basicsList.get(i);
            Field[] fields = basics.getClass().getDeclaredFields();
            for (int j = 4; j < rowName.length + 4; j++) {
                Field f = fields[j];
                f.setAccessible(true);
                XSSFCell cell = row.createCell(((short) j) - 4);
                cell.setCellStyle(style);
                if (f.get(basics) != null) {
                    cell.setCellValue(f.get(basics).toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }
    }

    private void insertPurchase(XSSFSheet sheet, String[] rowName, List<PurchaseExcel> purchaseList, XSSFCellStyle style) throws Exception {
        for (int i = 0; i < purchaseList.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            PurchaseExcel purchase = purchaseList.get(i);
            Field[] fields = purchase.getClass().getDeclaredFields();
            for (int j = 1; j < rowName.length + 1; j++) {
                Field f = fields[j];
                f.setAccessible(true);
                XSSFCell cell = row.createCell(((short) j) - 1);
                cell.setCellStyle(style);
                if (f.get(purchase) != null) {
                    cell.setCellValue(f.get(purchase).toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }
    }

    private void insertDailyProject(XSSFSheet sheet, String[] rowName, List<DailyAssetExcel> purchaseList, XSSFCellStyle style) throws Exception {
        for (int i = 0; i < purchaseList.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            DailyAssetExcel dailyAssetExcel = purchaseList.get(i);
            Field[] fields = dailyAssetExcel.getClass().getDeclaredFields();
            for (int j = 0; j < rowName.length; j++) {
                Field f = fields[j];
                f.setAccessible(true);
                XSSFCell cell = row.createCell((int) ((short) j));
                cell.setCellStyle(style);
                if (f.get(dailyAssetExcel) != null) {
                    cell.setCellValue(f.get(dailyAssetExcel).toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }
    }

    private void insertReport(XSSFSheet sheet, String[] rowName, List<Report> reportList, XSSFCellStyle style) throws Exception {
        int znsum = 0;
        int jcsum = 0;
        int cgsum = 0;
        for (int i = 0; i < reportList.size(); i++) {
            Report report = reportList.get(i);
            if ("智能".equals(report.getType())) {
                znsum++;
            } else if ("基础".equals(report.getType())) {
                jcsum++;
            } else if ("采购".equals(report.getType())) {
                cgsum++;
            }
        }
        if (znsum != 0) {
            if (znsum > 1) {
                sheet.addMergedRegion(new CellRangeAddress(1, znsum, 0, 0));
                if (jcsum != 0) {
                    if (jcsum > 1) {
                        sheet.addMergedRegion(new CellRangeAddress(znsum + 1, znsum + jcsum, 0, 0));
                        if (cgsum != 0 && cgsum > 1) {
                            sheet.addMergedRegion(new CellRangeAddress(znsum + jcsum + 1, znsum + jcsum + cgsum, 0, 0));
                        }
                    } else if (cgsum != 0 && cgsum > 1) {
                        sheet.addMergedRegion(new CellRangeAddress(znsum + 2, znsum + 1 + cgsum, 0, 0));
                    }
                } else if (cgsum != 0 && cgsum > 1) {
                    sheet.addMergedRegion(new CellRangeAddress(znsum + 1, znsum + cgsum, 0, 0));
                }
            } else if (jcsum != 0) {
                if (jcsum > 1) {
                    sheet.addMergedRegion(new CellRangeAddress(2, 1 + jcsum, 0, 0));
                    if (cgsum != 0 && cgsum > 1) {
                        sheet.addMergedRegion(new CellRangeAddress(2 + jcsum, 1 + jcsum + cgsum, 0, 0));
                    }
                } else if (cgsum != 0 && cgsum > 1) {
                    sheet.addMergedRegion(new CellRangeAddress(3, 2 + cgsum, 0, 0));
                }
            } else if (cgsum != 0 && cgsum > 1) {
                sheet.addMergedRegion(new CellRangeAddress(2, 1 + cgsum, 0, 0));
            }
        } else if (jcsum != 0) {
            if (jcsum > 1) {
                sheet.addMergedRegion(new CellRangeAddress(1, jcsum, 0, 0));
                if (cgsum != 0 && cgsum > 1) {
                    sheet.addMergedRegion(new CellRangeAddress(1 + jcsum, jcsum + cgsum, 0, 0));
                }
            } else if (cgsum != 0 && cgsum > 1) {
                sheet.addMergedRegion(new CellRangeAddress(2, 1 + cgsum, 0, 0));
            }
        } else if (cgsum != 0 && cgsum > 1) {
            sheet.addMergedRegion(new CellRangeAddress(1, cgsum, 0, 0));
        }
        for (int i2 = 0; i2 < reportList.size(); i2++) {
            XSSFRow row = sheet.createRow(i2 + 1);
            Report report2 = reportList.get(i2);
            Field[] fields = report2.getClass().getDeclaredFields();
            for (int j = 0; j < rowName.length; j++) {
                Field f = fields[j];
                f.setAccessible(true);
                XSSFCell cell = row.createCell((int) ((short) j));
                cell.setCellStyle(style);
                if (f.get(report2) != null) {
                    cell.setCellValue(f.get(report2).toString());
                } else {
                    cell.setCellValue(Const.default_value_double);
                }
            }
        }
    }

    private void insertDepRate(XSSFSheet sheet, String[] rowName, List<DepRate> depRateList, XSSFCellStyle style) throws IllegalAccessException {
        for (int i = 0; i < depRateList.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            DepRate depRate = depRateList.get(i);
            Field[] fields = depRate.getClass().getDeclaredFields();
            for (int j = 0; j < rowName.length; j++) {
                Field f = fields[j];
                f.setAccessible(true);
                XSSFCell cell = row.createCell((int) ((short) j));
                cell.setCellStyle(style);
                if (f.get(depRate) != null) {
                    cell.setCellValue(f.get(depRate).toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }
    }

    private String writeExcelToDisk(String filePath, XSSFWorkbook wb) {
        try {
            new File(filePath.substring(0, filePath.lastIndexOf("/"))).mkdirs();
            FileOutputStream fout = new FileOutputStream(filePath);
            wb.write(fout);
            fout.close();
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "导出失败！";
        }
    }
}